package cn.iocoder.yudao.module.polling.controller.admin.pollingcategories;

import cn.iocoder.yudao.module.polling.controller.admin.pollingcategories.vo.*;
import cn.iocoder.yudao.module.polling.controller.admin.pollingproject.vo.PollingProjectIOVO;
import cn.iocoder.yudao.module.polling.convert.pollingcategories.PollingCategoriesConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingcategories.PollingCategoriesDO;
import cn.iocoder.yudao.module.polling.service.pollingcategories.PollingCategoriesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Api(tags = "管理后台 - 适用类别")
@RestController
@RequestMapping("/abu/polling-categories")
@Validated
public class PollingCategoriesController {

    @Resource
    private PollingCategoriesService pollingCategoriesService;

    @PostMapping("/create")
    @ApiOperation("创建适用类别")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:create')")
    public CommonResult<Integer> createPollingCategories(@Valid @RequestBody PollingCategoriesCreateReqVO createReqVO) {
        return success(pollingCategoriesService.createPollingCategories(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新适用类别")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:update')")
    public CommonResult<Boolean> updatePollingCategories(@Valid @RequestBody PollingCategoriesUpdateReqVO updateReqVO) {
        pollingCategoriesService.updatePollingCategories(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除适用类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:delete')")
    public CommonResult<Boolean> deletePollingCategories(@RequestParam("id") Integer id) {
        pollingCategoriesService.deletePollingCategories(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得适用类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:query')")
    public CommonResult<PollingCategoriesRespVO> getPollingCategories(@RequestParam("id") Integer id) {
        PollingCategoriesDO pollingCategories = pollingCategoriesService.getPollingCategories(id);
        return success(PollingCategoriesConvert.INSTANCE.convert(pollingCategories));
    }

    @GetMapping("/list")
    @ApiOperation("获得适用类别列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:query')")
    public CommonResult<List<PollingCategoriesRespVO>> getPollingCategoriesList(@RequestParam("ids") Collection<Integer> ids) {
        List<PollingCategoriesDO> list = pollingCategoriesService.getPollingCategoriesList(ids);
        return success(PollingCategoriesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得适用类别分页")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:query')")
    public CommonResult<PageResult<PollingCategoriesRespVO>> getPollingCategoriesPage(@Valid PollingCategoriesPageReqVO pageVO) {
        PageResult<PollingCategoriesDO> pageResult = pollingCategoriesService.getPollingCategoriesPage(pageVO);
        return success(PollingCategoriesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出适用类别 Excel")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:export')")
    @OperateLog(type = EXPORT)
    public void exportPollingCategoriesExcel(@Valid PollingCategoriesExportReqVO exportReqVO,
                                             HttpServletResponse response) throws IOException {
        List<PollingCategoriesDO> list = pollingCategoriesService.getPollingCategoriesList(exportReqVO);
        // 导出 Excel
        List<PollingCategoriesExcelVO> datas = PollingCategoriesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "适用类别.xls", "数据", PollingCategoriesExcelVO.class, datas);
    }

    @GetMapping("/type")
    @ApiOperation("获得设备类别列表")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:etype')")
    public CommonResult<List<EquVo>> getEmpTypeList() {
        List<EquVo> list = pollingCategoriesService.selectAllEmpType();
        return success(list);
    }

    @GetMapping("/emq/{typeId}")
    @ApiOperation("获得设备列表")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:etype')")
    public CommonResult<List<EquVo>> getEmpList(@PathVariable("typeId") Integer typeId) {
        List<EquVo> list = pollingCategoriesService.selectAllEmp(typeId);
        return success(list);
    }

    @GetMapping("/emqbz/{typeId}")
    @ApiOperation("获得设备列表")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:etype')")
    public CommonResult<PageResult<EquInVo>> getEmpListBx(@PathVariable("typeId") Integer typeId) {
        EquInVo equInVo=new EquInVo();
        equInVo.setBzId(typeId);
        List<EquInVo> list = pollingCategoriesService.selectEquByBzId(equInVo);
        PageResult<EquInVo> pageResult=new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }

    @GetMapping("/user")
    @ApiOperation("获得用户列表")
    @PreAuthorize("@ss.hasPermission('abu:polling-categories:euser')")
    public CommonResult<List<EquVo>> getEmpList( String equName) {
        List<EquVo> list = pollingCategoriesService.selectUserByAll(equName);
        return success(list);
    }

}
